package com.example.mall.web.controller;

import com.example.mall.dto.AlipayNotifyDto;
import com.example.mall.dto.PayApplyDto;
import com.example.mall.result.JSONResult;
import com.example.mall.service.IPayService;
import com.example.mall.strategy.PayNotifyStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author: xxxliu
 * @CreateTime: 2025-06-03
 * @Version: 1.0
 */
@Slf4j
@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    public IPayService payService;

    @Autowired
    private Map<String, PayNotifyStrategy> payNotifyStrategyMap;

    @RequestMapping(value="/apply",method= RequestMethod.POST)
    public JSONResult apply(@RequestBody PayApplyDto payApplyDto){
        return JSONResult.success(payService.apply(payApplyDto));
    }

    @RequestMapping("/alipay/notify")
    public String alipayNotify(AlipayNotifyDto dto, HttpServletRequest request){
        try{
            log.info("支付结果处理：{}",dto);
            return payNotifyStrategyMap.get("AlipayNotifyStrategy").notify(dto);
        }catch (Exception e){
            e.printStackTrace();
            return "ERROR";
        }
    }
}
